﻿<Window x:Class="SqliteHelper.Windows.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Classes="clr-namespace:SqliteHelper.Classes"
        xmlns:MVVM="clr-namespace:Monti.MVVM;assembly=Monti"
        xmlns:montiControls="clr-namespace:Monti.Controls;assembly=Monti"
        Icon="/SqliteHelper;component/Images/database.ico"
        MVVM:CommandBehavior.Event="Closing"
        MVVM:CommandBehavior.Action="{Binding Path=ClosingAction}"
        Title="Sqlite Helper"
        Height="768"
        Width="1024">
   <Window.DataContext>
      <Classes:MainViewModel />
   </Window.DataContext>

   <Window.Resources>
      <ResourceDictionary>
         <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/SqliteHelper;component/Resources/Common.xaml" />
            <ResourceDictionary Source="/SqliteHelper;component/Resources/NewDatabaseView.xaml" />
            <ResourceDictionary Source="/SqliteHelper;component/Resources/SqliteTableView.xaml" />
            <ResourceDictionary Source="/SqliteHelper;component/Resources/CopyTableView.xaml" />
         </ResourceDictionary.MergedDictionaries>
      </ResourceDictionary>
   </Window.Resources>

   <Grid>
      <Grid.RowDefinitions>
         <RowDefinition Height="Auto" />
         <RowDefinition Height="*" />
         <RowDefinition Height="Auto" />
      </Grid.RowDefinitions>

      <Border Background="#FFA3AEB9"
              BorderThickness="0,0,0,1"
              BorderBrush="LightGray"
              Grid.Row="0">
         <StackPanel Orientation="Horizontal">
            <Button ToolTip="Create a new Database"
                    Style="{StaticResource _ToolBarButton}"
                    Command="{Binding Path=NewDatabaseCommand}">
               <Image Source="/SqliteHelper;component/Images/database_add.png"
                      Width="16"
                      Height="16" />
            </Button>
            <Button ToolTip="Open a Database"
                    Style="{StaticResource _ToolBarButton}"
                    Command="{Binding Path=OpenDatabaseCommand}">
               <Image Source="/SqliteHelper;component/Images/folder_database.png"
                      Width="16"
                      Height="16" />
            </Button>
            <Button ToolTip="Save current Database"
                    Style="{StaticResource _ToolBarButton}"
                    Command="{Binding Path=SaveDatabaseCommand}">
               <Image Source="/SqliteHelper;component/Images/database_save.png"
                      Width="16"
                      Height="16" />
            </Button>
            <Button ToolTip="Save all open Databases"
                    Style="{StaticResource _ToolBarButton}"
                    Command="{Binding Path=SaveAllDatabaseCommand}">
               <Image Source="/SqliteHelper;component/Images/database_save_all.png"
                      Width="16"
                      Height="16" />
            </Button>
            <Separator Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" />
            <Button ToolTip="Undo changes"
                    Style="{StaticResource _ToolBarButton}"
                    Command="{Binding Path=UndoDatabaseCommand}">
               <Image Source="/SqliteHelper;component/Images/database_undo.png"
                      Width="16"
                      Height="16" />
            </Button>
            <Button ToolTip="Close current database"
                    Style="{StaticResource _ToolBarButton}"
                    Command="{Binding Path=CloseDatabaseCommand}">
               <Image Source="/SqliteHelper;component/Images/database_delete.png"
                      Width="16"
                      Height="16" />
            </Button>
            <Separator Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" />
            <Button ToolTip="Add a new table"
                    Style="{StaticResource _ToolBarButton}"
                    Command="{Binding Path=NewTableCommand}">
               <Image Source="/SqliteHelper;component/Images/database_add_table.png"
                      Width="16"
                      Height="16" />
            </Button>
            <Button ToolTip="Copy a table"
                    Style="{StaticResource _ToolBarButton}"
                    Command="{Binding Path=CopyTableCommand}">
               <Image Source="/SqliteHelper;component/Images/database_table_edit.png"
                      Width="16"
                      Height="16" />
            </Button>
            <Button ToolTip="Drop the selected table"
                    Style="{StaticResource _ToolBarButton}"
                    Command="{Binding Path=DropTableCommand}">
               <Image Source="/SqliteHelper;component/Images/database_drop_table.png"
                      x:Name="_Image"
                      Width="16"
                      Height="16" />
            </Button>
            <Separator Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" />
            <ToggleButton ToolTip="Toggle SQL Input"
                          x:Name="_ToggleSqlInput"
                          Style="{StaticResource _ToolBarToggleButton}">
               <Image Source="/SqliteHelper;component/Images/database_lightning.png"
                      Width="16"
                      Height="16" />
            </ToggleButton>
         </StackPanel>
      </Border>

      <Grid Grid.Row="1">
         <Grid.ColumnDefinitions>
            <ColumnDefinition Width=".2*" />
            <ColumnDefinition Width="3px" />
            <ColumnDefinition Width=".8*" />
         </Grid.ColumnDefinitions>
         
         <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
         </Grid.RowDefinitions>

         <Grid Grid.Column="0"
               Grid.Row="0">
            <Grid.RowDefinitions>
               <RowDefinition Height="Auto" />
               <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Border Background="{StaticResource _DialogBackground}"
                    Grid.Row="0">
               <TextBlock Text="Open Databases"
                          Foreground="White"
                          Margin="5,0,0,0"
                          FontSize="14"/>
            </Border>
            <ListBox ItemsSource="{Binding Path=DatabaseList}"
                     SelectedItem="{Binding Path=SelectedDatabase}"
                     AllowDrop="True"
                     MVVM:CommandBehavior.Event="Drop"
                     MVVM:CommandBehavior.Command="{Binding Path=DropCommand}"
                     x:Name="_DatabaseList"
                     Grid.Row="1">
               <ListBox.ItemContainerStyle>
                  <Style TargetType="ListBoxItem">
                     <Setter Property="ToolTip"
                             Value="{Binding Path=FilePath, Mode=OneWay}" />
                     <Setter Property="IsSelected"
                             Value="{Binding Path=IsSelected, Mode=TwoWay}" />
                     <Setter Property="Template">
                        <Setter.Value>
                           <ControlTemplate TargetType="ListBoxItem">
                              <Border x:Name="_BdrMain"
                                      BorderBrush="{StaticResource _HighlightBorderBrush}"
                                      BorderThickness="0"
                                      Background="Transparent">
                                 <Grid>
                                    <Grid.RowDefinitions>
                                       <RowDefinition Height="Auto" />
                                       <RowDefinition Height="Auto" />
                                    </Grid.RowDefinitions>

                                    <StackPanel Orientation="Horizontal"
                                                Grid.Row="0">
                                       <TextBlock Text="*"
                                                  Margin="3,0,0,0"
                                                  Visibility="{Binding Path=NeedsSaving, Converter={StaticResource bool_visi}}" />
                                       
                                       <TextBlock Text="{Binding Path=FileName, Mode=OneWay}" />
                                    </StackPanel>

                                    <TextBlock Grid.Row="1">
                                       <Run Text="{Binding Path=FileSize, Mode=OneWay}" />
                                       <Run Text="kb" />
                                    </TextBlock>
                                 </Grid>
                              </Border>

                              <ControlTemplate.Triggers>
                                 <Trigger Property="IsSelected"
                                          Value="True">
                                    <Setter Property="FontWeight"
                                            Value="Bold" />
                                    <Setter TargetName="_BdrMain"
                                            Property="BorderThickness"
                                            Value="1" />
                                    <Setter TargetName="_BdrMain"
                                            Property="Background"
                                            Value="{StaticResource _HighlightBackground}" />
                                 </Trigger>
                              </ControlTemplate.Triggers>
                           </ControlTemplate>
                        </Setter.Value>
                     </Setter>
                  </Style>
               </ListBox.ItemContainerStyle>
            </ListBox>
         </Grid>

         <Grid Grid.Column="2"
               Grid.Row="0">
            <Grid.RowDefinitions>
               <RowDefinition Height="Auto" />
               <RowDefinition Height="*" />
               <RowDefinition Height="Auto" />
               <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <Grid Grid.Row="0"
                  Background="{StaticResource _TitleBarBackground}"
                  DataContext="{Binding Path=SelectedDatabase}">
               <Grid.RowDefinitions>
                  <RowDefinition Height="Auto" />
                  <RowDefinition Height="Auto" />
               </Grid.RowDefinitions>
               
               <TextBlock Text="{Binding Path=FileName}"
                          FontSize="14"
                          Grid.Row="0"
                          FontWeight="Bold" />

               <TextBlock Text="{Binding Path=FilePath, Mode=OneWay}"
                          VerticalAlignment="Center"
                          Grid.Row="1"
                          Margin="0,0,4,0"
                          FontStyle="Italic" />
            </Grid>

            <Grid Grid.Row="1"
                  DataContext="{Binding Path=SelectedDatabase}">
               <Grid.ColumnDefinitions>
                  <ColumnDefinition Width=".2*" />
                  <ColumnDefinition Width="3px" />
                  <ColumnDefinition Width=".8*" />
               </Grid.ColumnDefinitions>

               <Grid Grid.Column="0">
                  <Grid.RowDefinitions>
                     <RowDefinition Height="Auto" />
                     <RowDefinition Height="*" />
                  </Grid.RowDefinitions>
                  <Border Background="{StaticResource _DialogBackground}"
                          Grid.Row="0">
                     <TextBlock Text="Tables"
                                Foreground="White"
                                Margin="5,0,0,0"
                                FontSize="14" />
                  </Border>

                  <montiControls:TreeViewEx Grid.Row="1"
                                            ItemsSource="{Binding Path=TableList}"
                                            SelectedTreeItem="{Binding Path=SelectedTable, Mode=TwoWay}">
                     <montiControls:TreeViewEx.Resources>
                        <HierarchicalDataTemplate DataType="{x:Type MVVM:TreeViewItemViewModel}"
                                                  ItemsSource="{Binding Path=Children}">
                           <StackPanel Orientation="Horizontal">
                              <TextBlock Text="{Binding Path=Text}" />
                              <TextBlock Text="{Binding Path=Tag}">
                                 <TextBlock.Resources>
                                    <Style TargetType="TextBlock">
                                       <Style.Triggers>
                                          <DataTrigger Binding="{Binding Path=Tag}"
                                                       Value="{x:Null}">
                                             <Setter Property="Visibility"
                                                     Value="Collapsed" />
                                          </DataTrigger>
                                       </Style.Triggers>
                                    </Style>
                                 </TextBlock.Resources>
                                 <Run Text=" - " />
                                 <Run Text="{Binding Path=Tag}" />
                              </TextBlock>
                           </StackPanel>
                        </HierarchicalDataTemplate>
                     </montiControls:TreeViewEx.Resources>
                  </montiControls:TreeViewEx>
               </Grid>

               <Grid Grid.Column="2">
                  <Grid.RowDefinitions>
                     <RowDefinition Height="*" />
                     <RowDefinition Height="Auto" />
                  </Grid.RowDefinitions>
                  <DataGrid ItemsSource="{Binding Path=Data}"
                            Grid.Row="0">
                     <MVVM:CommandBehaviorCollection.Behaviors>
                        <MVVM:CommandBehavior Event="SelectionChanged"
                                              Command="{Binding Path=SelectionChangedCommand}" />
                        <MVVM:CommandBehavior Event="CellEditEnding"
                                              Command="{Binding Path=EndCellEditCommand}" />
                     </MVVM:CommandBehaviorCollection.Behaviors>
                  </DataGrid>
                            <!--
                            MVVM:CommandBehavior.Event="CellEditEnding"
                            MVVM:CommandBehavior.Command="{Binding Path=EndCellEditCommand}" /-->
                  
                  <StackPanel Orientation="Horizontal"
                              Grid.Row="1"
                              Margin="5"
                              HorizontalAlignment="Right">
                     <Button Margin="0,0,5,0"
                             Style="{StaticResource _ToolBarButton}"
                             Command="{Binding Path=AddRowCommand}">
                        <StackPanel Orientation="Horizontal">
                           <Image Source="/SqliteHelper;component/Images/database_add_row.png"
                                  Width="16"
                                  Height="16" />
                           <TextBlock Text="Add Row"
                                      Margin="3,0,0,0" />
                        </StackPanel>
                     </Button>
                     <Button Style="{StaticResource _ToolBarButton}"
                             Command="{Binding Path=DeleteSelectionCommand}">
                        <StackPanel Orientation="Horizontal">
                        <Image Source="/SqliteHelper;component/Images/database_delete_rows.png"
                               Width="16"
                               Height="16" />
                           <TextBlock Text="Delete Selection"
                                      Margin="3,0,0,0" />
                        </StackPanel>
                     </Button>
                  </StackPanel>
               </Grid>
               
               <GridSplitter Grid.Column="1"
                             HorizontalAlignment="Stretch"
                             VerticalAlignment="Stretch"
                             Width="3" />
            </Grid>

            <GridSplitter Grid.Row="2"
                          HorizontalAlignment="Stretch"
                          VerticalAlignment="Stretch"
                          Height="3"
                          Visibility="{Binding Path=IsChecked, ElementName=_ToggleSqlInput, Converter={StaticResource bool_visi}}" />

            <Grid Grid.Row="3"
                  Visibility="{Binding Path=IsChecked, ElementName=_ToggleSqlInput, Converter={StaticResource bool_visi}}">
               <Grid.RowDefinitions>
                  <RowDefinition Height="Auto" />
                  <RowDefinition Height="*" />
                  <RowDefinition Height="Auto" />
               </Grid.RowDefinitions>
               <Border Background="{StaticResource _DialogBackground}"
                       Grid.Row="0">
                  <TextBlock Text="SQL Input"
                             Foreground="White"
                             Margin="5,0,0,0"
                             FontSize="14" />
               </Border>
               
               <TextBox Grid.Row="1"
                        TextWrapping="Wrap"
                        Text="{Binding Path=SqlText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
               
               <Grid Grid.Row="2">
                  <Grid.ColumnDefinitions>
                     <ColumnDefinition Width="Auto" />
                     <ColumnDefinition Width="Auto" />
                     <ColumnDefinition Width="*" />
                  </Grid.ColumnDefinitions>

                  <CheckBox Grid.Column="0"
                            VerticalAlignment="Center"
                            ToolTip="Run SQL statement on all open Databases"
                            IsChecked="{Binding Path=RunSqlOnAllDatabases}">
                     <Image Source="/SqliteHelper;component/Images/database_multiple.png"
                            Width="14"
                            Height="14" />
                  </CheckBox>
                  
                  <Button Grid.Column="1"
                          Command="{Binding Path=ExecuteSqlCommand}"
                          Margin="4,0">
                     <StackPanel Orientation="Horizontal">
                        <Image Source="/SqliteHelper;component/Images/database_lightning.png"
                               Width="16"
                               Height="16" />
                        <TextBlock Text="Execute SQL"
                                   Margin="2,0,0,0"/>
                     </StackPanel>
                  </Button>

                  <TextBlock Grid.Column="2"
                             TextWrapping="Wrap"
                             Text="{Binding Path=SqlResult}" />
               </Grid>
            </Grid>

         </Grid>

         <GridSplitter Grid.Column="1"
                       Grid.Row="0"
                       HorizontalAlignment="Stretch"
                       VerticalAlignment="Stretch"
                       Width="3" />
      </Grid>

      <StatusBar Grid.Row="2">
         <StatusBar.ItemsPanel>
            <ItemsPanelTemplate>
               <Grid>
                  <Grid.RowDefinitions>
                     <RowDefinition Height="*" />
                  </Grid.RowDefinitions>
                  <Grid.ColumnDefinitions>
                     <ColumnDefinition Width="*" />
                     <ColumnDefinition Width="Auto" />
                  </Grid.ColumnDefinitions>
               </Grid>
            </ItemsPanelTemplate>
         </StatusBar.ItemsPanel>
         <StatusBarItem Grid.Column="0">
            <TextBlock Text="{Binding Path=StatusText}" />
         </StatusBarItem>
      </StatusBar>

      <montiControls:MainWindowDialog Grid.RowSpan="2" />
   </Grid>
</Window>
